package hhm.android.my

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProvider
import hhm.android.library.utils.BackListener
import hhm.android.library.utils.MatchUtils
import hhm.android.library.utils.OnMultiClickListener
import siau.android.base.SBBaseActivity

class ChangePhoneActivity : SBBaseActivity(), TextWatcher {

    lateinit var viewModel: ChangePhoneViewModel
    lateinit var etPhone: EditText
    lateinit var etCode: EditText
    lateinit var getCode: TextView
    lateinit var btn: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        setContentView(R.layout.activity_change_phone)
        initTitleWithLine(getString(R.string.bind_new_phone))

        viewModel = ViewModelProvider(this).get(ChangePhoneViewModel::class.java)

        etPhone = findViewById(R.id.activity_change_phone_account_et)
        etCode = findViewById(R.id.activity_change_phone_code_et)
        getCode = findViewById(R.id.activity_change_phone_get_verify_code)
        btn = findViewById(R.id.activity_change_phone_btn)

        etPhone.addTextChangedListener(this)
        etCode.addTextChangedListener(this)

        getCode.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (viewModel.canGetCode) {
                    if (!viewModel.hasGetCode.value!!) {
                        viewModel.hasGetCode.value = true
                        viewModel.startCountDown(lifecycle(), etPhone.text.toString())
                    }
                } else {
                    showToast(getString(R.string.please_input_true_phone))
                }
            }
        })
        btn.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (viewModel.canBtn.value == true) {
                    showLoadingFragment(R.id.activity_change_phone_fl)
                    viewModel.changePhone(etPhone.text.toString(), etCode.text.toString(), object : BackListener {
                        override fun callBack() {
                            removeLoadingFragment()
                            setResult(1024)
                            finish()
                        }
                    })
                }
            }
        })

        viewModel.countDownNumber.observe(this, {
            if (it == 60) {
                getCode.text = getString(R.string.get_verify_code)
            } else {
                removeLoadingFragment()
                getCode.text = getString(R.string.re_get_verify_code_1) + it + getString(R.string.re_get_verify_code_2)
            }
        })

        viewModel.canBtn.observe(this, {
            if (it) {
                btn.background = ContextCompat.getDrawable(this, R.drawable.rectangle_circle_maincolor)
            } else {
                btn.background = ContextCompat.getDrawable(this, R.drawable.rectangle_circle_grey_e6)
            }
        })

        viewModel.errorString.observe(this) {
            removeLoadingFragment()
            showToast(it)
        }
    }


    override fun finish() {
        super.finish()
        viewModel.finish()
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        viewModel.canGetCode = MatchUtils.isMobile(etPhone.text.toString())
        viewModel.canBtn.value = MatchUtils.isMobile(etPhone.text.toString()) && etCode.length() >= 6
    }

    override fun afterTextChanged(s: Editable?) {}
}